#include <cstring> // for strncpy, memcpy, memset
#include <numbers> // for inv_pi, pi
-#include "defs.h" // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, xcalloc, track_add_wpt, xstrndup
+#include "defs.h" // for Waypoint, be_read32, be_read16, be_write32, fatal, be_write16, route_head, track_add_wpt
#include "mkshort.h" // for MakeShort
#include "src/core/datetime.h" // for DateTime
void
HumminbirdBase::humminbird_read_wpt(gbfile* fin)
{
- humminbird_waypt_t w;
+ humminbird_waypt_t w{0};
if (! gbfread(&w, 1, sizeof(w), fin)) {
fatal(MYNAME ": Unexpected end of file!\n");
auto* wpt = new Waypoint;
- // Could probably find a way to eliminate the alloc/copy.
- char* s = xstrndup(w.name, sizeof(w.name));
- wpt->shortname = s;
- xfree(s);
-
+ wpt->shortname = QByteArray(w.name, static_cast<int>(qstrnlen(w.name, sizeof(w.name))));
wpt->SetCreationTime(w.time);
double guder = gudermannian_i1924(w.north);
wpt->latitude = geocentric_to_geodetic_hwr(guder);
- wpt->longitude = (double)w.east / EAST_SCALE * 180.0;
+ wpt->longitude = static_cast<double>(w.east) / EAST_SCALE * 180.0;
wpt->altitude = 0.0; /* It's from a fishfinder... */
if (w.depth != 0) {
- wpt->set_depth((double)w.depth / 100.0);
+ wpt->set_depth(static_cast<double>(w.depth) / 100.0);
}
- int num_icons = sizeof(humminbird_icons) / sizeof(humminbird_icons[0]);
+ int num_icons = std::size(humminbird_icons);
if (w.icon < num_icons) {
wpt->icon_descr = humminbird_icons[w.icon];
}
HumminbirdBase::humminbird_read_route(gbfile* fin) const
{
- humminbird_rte_t hrte;
+ humminbird_rte_t hrte{0};
if (! gbfread(&hrte, 1, sizeof(hrte), fin)) {
fatal(MYNAME ": Unexpected end of file!\n");
if (rte == nullptr) {
rte = new route_head;
route_add_head(rte);
- // TODO: find a way to eliminate the copy.
- char* s = xstrndup(hrte.name, sizeof(hrte.name));
- rte->rte_name = s;
- xfree(s);
- /* rte->rte_num = hrte.num + 1; only internal number */
+ rte->rte_name = QByteArray(hrte.name, static_cast<int>(qstrnlen(hrte.name, sizeof(hrte.name))));
}
route_add_wpt(rte, new Waypoint(*wpt));
}
HumminbirdBase::humminbird_read_track(gbfile* fin)
{
- humminbird_trk_header_t th;
+ humminbird_trk_header_t th{0};
if (! gbfread(&th, 1, sizeof(th), fin)) {
fatal(MYNAME ": Unexpected end of file reading header!\n");
/* num_points is actually one too big, because it includes the value in
the header. But we want the extra point at the end because the
freak-value filter below looks at points[i+1] */
- auto* points = (humminbird_trk_point_t*) xcalloc(th.num_points, sizeof(humminbird_trk_point_t));
+ auto* points = new humminbird_trk_point_t[th.num_points]();
if (! gbfread(points, sizeof(humminbird_trk_point_t), th.num_points-1, fin)) {
fatal(MYNAME ": Unexpected end of file reading points!\n");
}
auto* trk = new route_head;
track_add_head(trk);
- // TODO: find a way to eliminate the copy.
- char* s = xstrndup(th.name, sizeof(th.name));
- trk->rte_name = s;
- xfree(s);
+ trk->rte_name = QByteArray(th.name, static_cast<int>(qstrnlen(th.name, sizeof(th.name))));
trk->rte_num = th.trk_num;
/* We create one wpt for the info in the header */
/* No depth info in the header. */
track_add_wpt(trk, first_wpt);
- for (int i = 0 ; i<th.num_points-1 ; i++) {
+ for (int i = 0 ; i < th.num_points-1 ; i++) {
auto* wpt = new Waypoint;
points[i].depth = be_read16(&points[i].depth);
wpt->altitude = 0.0;
if (points[i].depth != 0) {
- wpt->set_depth((double)points[i].depth / 100.0);
+ wpt->set_depth(static_cast<double>(points[i].depth) / 100.0);
}
if (i == th.num_points-2 && th.time != 0) {
}
track_add_wpt(trk, wpt);
}
- xfree(points);
+ delete[] points;
}
void
HumminbirdBase::humminbird_read_track_old(gbfile* fin)
{
- humminbird_trk_header_old_t th;
+ humminbird_trk_header_old_t th{0};
constexpr int file_len = 8048;
char namebuf[TRK_NAME_LEN];
/* num_points is actually one too big, because it includes the value in
the header. But we want the extra point at the end because the
freak-value filter below looks at points[i+1] */
- auto* points = (humminbird_trk_point_old_t*)xcalloc(th.num_points, sizeof(humminbird_trk_point_old_t));
+ auto* points = new humminbird_trk_point_old_t[th.num_points]();
if (! gbfread(points, sizeof(humminbird_trk_point_old_t), th.num_points-1, fin)) {
fatal(MYNAME ": Unexpected end of file reading points!\n");
}
gbfseek(fin, file_len-TRK_NAME_LEN, SEEK_SET);
gbfread(&namebuf, 1, TRK_NAME_LEN, fin);
- trk->rte_name = xstrndup(namebuf, sizeof(namebuf));
+ trk->rte_name = QByteArray(namebuf, static_cast<int>(qstrnlen(namebuf, sizeof(namebuf))));
trk->rte_num = th.trk_num;
/* We create one wpt for the info in the header */
}
track_add_wpt(trk, wpt);
}
- xfree(points);
+ delete[] points;
}
void
HumminbirdBase::humminbird_read()
{
while (! gbfeof(fin_)) {
- uint32_t signature = gbfgetuint32(fin_);
-
- switch (signature) {
+ switch (uint32_t signature = gbfgetuint32(fin_)) {
case WPT_MAGIC:
case WPT_MAGIC2:
humminbird_read_wpt(fin_);
void
HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
{
- humminbird_waypt_t hum;
- int num_icons = sizeof(humminbird_icons) / sizeof(humminbird_icons[0]);
+ humminbird_waypt_t hum{0};
+ int num_icons = std::size(humminbird_icons);
be_write16(&hum.num, waypoint_num++);
hum.zero = 0;
trk_head = nullptr;
last_time = 0;
if (!trk->rte_waypt_empty()) {
- trk_head = (humminbird_trk_header_t*) xcalloc(1, sizeof(humminbird_trk_header_t));
- trk_points = (humminbird_trk_point_t*) xcalloc(max_points, sizeof(humminbird_trk_point_t));
-
+ trk_head = new humminbird_trk_header_t();
+ trk_points = new humminbird_trk_point_t[max_points]();
QString name = trkname_sh->mkshort(trk->rte_name);
strncpy(trk_head->name, CSTR(name), sizeof(trk_head->name)-1);
be_write16(&trk_head->trk_num, trk->rte_num);
gbfwrite(trk_points, max_points, sizeof(humminbird_trk_point_t), fout_);
gbfputuint16(0, fout_); /* Odd but true. The format doesn't fit an int nr of entries. */
- xfree(trk_head);
- xfree(trk_points);
+ delete trk_head;
+ delete[] trk_points;
trk_head = nullptr;
trk_points = nullptr;
} else {
/* These points are 16-bit differential. */
int j = i-1;
- trk_points[j].deltaeast = east - last_east;
- trk_points[j].deltanorth = north - last_north;
+ trk_points[j].deltaeast = static_cast<int16_t>(east - last_east);
+ trk_points[j].deltanorth = static_cast<int16_t>(north - last_north);
trk_points[j].depth = qRound(wpt->depth_value_or(0) * 100.0);
/* BE-ify */
{
humrte = nullptr;
if (!rte->rte_waypt_empty()) {
- humrte = (humminbird_rte_t*) xcalloc(1, sizeof(*humrte));
+ humrte = new humminbird_rte_t;
}
}
gbfwrite(humrte, sizeof(*humrte), 1, fout_);
}
- xfree(humrte);
+ delete humrte;
humrte = nullptr;
}